package com.lht.syt.order.api;

import com.lht.syt.common.result.Result;
import com.lht.syt.enums.PaymentTypeEnum;
import com.lht.syt.order.service.PaymentService;
import com.lht.syt.order.service.WeixinService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @author lht
 * @version 1.0
 * @description: TODO
 * @date 2024/11/18 17:31
 */
@Api(tags = "微信支付")
@RestController
@RequestMapping("api/order/weixin")
public class WeixinController {
    @Autowired
    private WeixinService weixinService;

    @Autowired
    private PaymentService paymentService;

    @ApiOperation(value = "根据订单下单，生成二维码")
    @GetMapping("/createNative/{orderId}")
    public Result createNative(@ApiParam(name = "orderId", value = "订单编号", required = true)  @PathVariable Long orderId){
        return Result.ok(weixinService.createNative(orderId));
    }

    @ApiOperation(value = "查询支付状态")
    @GetMapping("/queryPayStatus/{orderId}")
    public Result queryPayStatus(
            @ApiParam(name = "orderId", value = "订单id", required = true)
            @PathVariable("orderId") Long orderId) {
        //调用查询接口
        Map<String, String> resultMap = weixinService.queryPayStatus(orderId, PaymentTypeEnum.WEIXIN.name());
        if (resultMap == null) {//出错
            return Result.fail().message("支付出错");
        }
        if ("SUCCESS".equals(resultMap.get("trade_state"))) {//如果成功
            //更改订单状态，处理支付结果
            String out_trade_no = resultMap.get("out_trade_no");
            paymentService.paySuccess(out_trade_no, PaymentTypeEnum.WEIXIN.getStatus(), resultMap);
            return Result.ok().message("支付成功");
        }
        return Result.ok().message("支付中");
    }
}
